package Aleehot100;

/**
 * ClassName: c071
 * Package: Aleehot100
 * Description:394. 字符串解码
 *
 * @Author BCXJ
 * @Create 2025/8/17 11:24
 * @Version 1.0
 * @Since 1.0
 */
public class c071 {
    public static void main(String[] args) {
        System.out.println(decodeString("3[a]2[bc]"));
    }

    static int where = 0;

    public static String decodeString(String s) {
        where = 0;
        return process(s.toCharArray());
    }

    private static String process(char[] charArray) {
        int cur = 0 ;
        StringBuilder res = new StringBuilder();
        while(where < charArray.length && charArray[where] != ']') {
            if(charArray[where] >= 'a' &&  charArray[where] <= 'z') {
                res.append(charArray[where]);
                where ++;
            } else if (charArray[where] >= '0' && charArray[where] <= '9') {
                cur = cur * 10 + (charArray[where] - '0');
                where ++;
            } else {
                // 此时是 '[' 字符
                where ++;
                appends(process(charArray), cur, res);
                cur = 0;
            }

        }
        where ++;
        return res.toString();
    }

    private static void appends(String process, int cur, StringBuilder res) {
        for (int i = 0; i < cur; i++) {
            res.append(process);
        }
    }
}
